﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:PeerReviewVoter
    /// </summary>
    public partial class PeerReviewVoter
    {
        public PeerReviewVoter()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FPeerReviewID, string FEntityNo, string FVoterDeptNo, string FVoterEmplNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_PeerReviewVoter");
            strSql.Append(" where FPeerReviewID=@FPeerReviewID and FEntityNo=@FEntityNo and FVoterDeptNo=@FVoterDeptNo and FVoterEmplNo=@FVoterEmplNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterEmplNo", SqlDbType.NVarChar,15)			};
            parameters[0].Value = FPeerReviewID;
            parameters[1].Value = FEntityNo;
            parameters[2].Value = FVoterDeptNo;
            parameters[3].Value = FVoterEmplNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.PeerReviewVoter GetModel(decimal FPeerReviewID, string FEntityNo, string FVoterDeptNo, string FVoterEmplNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FPeerReviewID,FEntityNo,FVoterDeptNo,FVoterEmplNo,FStatus,FVoteDate,FVoteIP from t_PeerReviewVoter ");
            strSql.Append(" where FPeerReviewID=@FPeerReviewID and FEntityNo=@FEntityNo and FVoterDeptNo=@FVoterDeptNo and FVoterEmplNo=@FVoterEmplNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterEmplNo", SqlDbType.NVarChar,15)			};
            parameters[0].Value = FPeerReviewID;
            parameters[1].Value = FEntityNo;
            parameters[2].Value = FVoterDeptNo;
            parameters[3].Value = FVoterEmplNo;

            ElingOA6_Admin_DSJCJ.Model.PeerReviewVoter model = new ElingOA6_Admin_DSJCJ.Model.PeerReviewVoter();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.PeerReviewVoter DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.PeerReviewVoter model = new ElingOA6_Admin_DSJCJ.Model.PeerReviewVoter();
            if (row != null)
            {
                if (row["FPeerReviewID"] != null && row["FPeerReviewID"].ToString() != "")
                {
                    model.FPeerReviewID = decimal.Parse(row["FPeerReviewID"].ToString());
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FVoterDeptNo"] != null)
                {
                    model.FVoterDeptNo = row["FVoterDeptNo"].ToString();
                }
                if (row["FVoterEmplNo"] != null)
                {
                    model.FVoterEmplNo = row["FVoterEmplNo"].ToString();
                }
                if (row["FStatus"] != null)
                {
                    model.FStatus = row["FStatus"].ToString();
                }
                if (row["FVoteDate"] != null && row["FVoteDate"].ToString() != "")
                {
                    model.FVoteDate = DateTime.Parse(row["FVoteDate"].ToString());
                }
                if (row["FVoteIP"] != null)
                {
                    model.FVoteIP = row["FVoteIP"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FPeerReviewID,FEntityNo,FVoterDeptNo,FVoterEmplNo,FStatus,FVoteDate,FVoteIP ");
            strSql.Append(" FROM dbo.t_PeerReviewVoter ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

       
        #endregion  BasicMethod
        #region  ExtensionMethod

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public DataSet GetListByEntityNo(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FEmpName,deptName,b.* from v_HRDeptEmpl a,dbo.t_PeerReviewVoter b ");
            strSql.Append(" where a.FEmplNo=b.FVoterEmplNo and a.FDeptNo=b.FVoterDeptNo and a.FEntityNo=@FEntityNo and FIsMainPosition='Y'");
            strSql.Append(" order by FDeptNo,FSeq,FDutyNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = entityNo;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllList(string entityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.FPositionNo, a.FDeptNo, a.FEntityNo, a.FEmplNo, b.FEmpName, b.FSeq from t_HREmplPosition a, t_HREmpl b ");
            strSql.Append(" where a.FEmplNo = b.FEmplNo AND a.FEntityNo=@FEntityNo AND FIsMainPosition='Y' AND FEmpStatus <> '99' ");
            strSql.Append(" order by FSeq asc");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = entityNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="peerReviewBeenVotorModelList"></param>
        /// <param name="peerReviewID"></param>
        public void Add(List<Model.PeerReviewVoter> peerReviewVotorModelList, decimal peerReviewID)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            //删除当前主题下的全部删除
            strSql.Append("delete from dbo.t_PeerReviewVoter ");
            strSql.Append(" where FPeerReviewID=@FPeerReviewID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13)};
            parameters[0].Value = peerReviewID;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            //添加数据
            foreach (Model.PeerReviewVoter model in peerReviewVotorModelList)
            {
                strSql = new StringBuilder();
                cmds = new CommandInfo();
                strSql.Append("insert into dbo.t_PeerReviewVoter(");
                strSql.Append("FPeerReviewID,FEntityNo,FVoterDeptNo,FVoterEmplNo,FStatus,FVoteDate,FVoteIP)");
                strSql.Append(" values (");
                strSql.Append("@FPeerReviewID,@FEntityNo,@FVoterDeptNo,@FVoterEmplNo,@FStatus,@FVoteDate,@FVoteIP)");
                SqlParameter[] parameters1 = {
					new SqlParameter("@FPeerReviewID", SqlDbType.Decimal,13),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FVoterEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FStatus", SqlDbType.NVarChar,1),
					new SqlParameter("@FVoteDate", SqlDbType.DateTime),
					new SqlParameter("@FVoteIP", SqlDbType.NVarChar,15)};
                parameters1[0].Value = model.FPeerReviewID;
                parameters1[1].Value = model.FEntityNo;
                parameters1[2].Value = model.FVoterDeptNo;
                parameters1[3].Value = model.FVoterEmplNo;
                parameters1[4].Value = model.FStatus;
                parameters1[5].Value = DBNull.Value;
                parameters1[6].Value = DBNull.Value; 

                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters1;
                cmdList.Add(cmds);
            }


            DbHelperSQL.ExecuteSqlTranWithString(cmdList);  

        }

        #endregion  ExtensionMethod
    }
}

